import { GameData } from '../game/GameData';
import { EnergyMgr } from '../component/EnergyMgr';
import { EventKey } from '../common/Key';
cc.Class({
    extends: require('BaseScene'),

    properties: {
        nodBtnNewGame: cc.Node,
        labVersion: cc.Label,
    },

    onLoad() {
        this._super();
    },

    start() {
        G.partner.sendEvent('场景', { '类型': 'home' });
        GameData.getLocalLevel();
        this._registEvent();
        this._initVersion();
        G.partner.createGameClub();

        let action = cc.repeatForever(
            cc.sequence(
                cc.rotateTo(0.1, -20),
                cc.rotateTo(0.2, 20),
                cc.rotateTo(0.2, -20),
                cc.rotateTo(0.2, 20),
                cc.rotateTo(0.1, 0),
                cc.delayTime(0.2)
            ))
        this.nodBtnNewGame.runAction(action)
    },

    _initVersion() {
        this.labVersion.string = G.config.getVersion();
    },

    onDestroy() {
        this._unRegistEvent();
        G.partner.hideGameClub();
    },

    _registEvent() {
        G.onFire.on(EventKey.EVENT_ENTER_PANEL, this._onFire, this);
        G.onFire.on(EventKey.EVENT_EXIT_PANEL, this._onFire, this);

    },
    _unRegistEvent() {
        G.onFire.off(EventKey.EVENT_ENTER_PANEL, this._onFire, this);
        G.onFire.off(EventKey.EVENT_EXIT_PANEL, this._onFire, this);
    },

    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_ENTER_PANEL) {
            G.partner.hideGameClub();
        } else if (eventName == EventKey.EVENT_EXIT_PANEL) {
            G.partner.showGameClub();
        }
    },

    buttonListener(button) {
        let name = button.node.name;
        if (name == 'btnNewGame') {
            let result = EnergyMgr.subEnergy();
            if (!result) {
                G.game.toast('能量不足', 0.5)
                return;
            }
            GameData.resetLevel();
            G.game.loadScene('SceneGame');
        } else if (name == 'btnContinue') {
            let result = EnergyMgr.subEnergy();
            if (!result) {
                G.game.toast('能量不足', 0.5)
                return;
            }
            G.game.loadScene('SceneGame');
        } else if (name == 'btnShare') {
            G.partner.sendEvent('分享', { '类型': 'home' });
            G.partner.share({ title: '这一关你能闯过去吗？' }, () => {
                let result = EnergyMgr.addEnergy();
                if (result) {
                    G.game.toast('增加1点能量', 0.6);
                }
            });
        } else if (name == 'btnRank') {
            G.partner.sendEvent('打开弹窗', { '类型': '排行榜' });
            G.ui.openUI('PanelRank');
        }

    },

    // update (dt) {},
});
